package com.amos.cmp_android.presenter;

import android.content.Context;

import com.amos.cmp_android.constant.URLConstant;
import com.amos.cmp_android.model.NewsModel;
import com.amos.cmp_android.net.STError;
import com.amos.cmp_android.net.STHttpCallback;
import com.amos.cmp_android.net.STNetTask;
import com.amos.cmp_android.net.STRequest;
import com.amos.cmp_android.utils.GsonUtil;

import org.json.JSONObject;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by tlh on 2016/4/10.
 */
public class GetNewsPresenter extends BasePresenter {

    private GetNewsListener listener;
    private List<NewsModel> list;

    public GetNewsPresenter(Context context) {
        super(context);
    }

    public void setListener(GetNewsListener listener, List<NewsModel> list) {
        this.listener = listener;
        this.list = list;
    }

    public void getNews(int page, int num) {
        Map<String, Object> params = new HashMap<>();
        STRequest request;
        params.put("page", page);
        params.put("num", num);

        request = new STRequest(URLConstant.GET_NEWS, params, null);
        STNetTask.post(request, new STHttpCallback<JSONObject>() {
            @Override
            public void onSuccess(String response) {
                list.clear();
                GsonUtil.fromJson(response,NewsModel.class,list);
                if (listener!=null)
                    listener.onSuccess();
            }

            @Override
            public void onFailed(STError error) {
                if (listener != null)
                    listener.onFailed(error);
            }
        }, beforeQueryHook, afterQueryHook);

    }
    public interface GetNewsListener{
        public void onSuccess();
        public void onFailed(STError error);
    }
}
